/*
2. List Leaves 训练建树和遍历基本功，一定要做；

*/

#include <stdio.h>
#include <stdlib.h>

#define maxsize 10

struct Tree {
    int data;
    int left;
    int right;
} T[maxsize];

struct Node {
    int treeIndex;
    struct Node *next;
};

struct QNode {
    struct Node *head;
    struct Node *rear;
};
typedef struct QNode* qptr;

int isEmpty(qptr Q) {
    if ((Q->head == NULL) && (Q->rear==NULL)) {
        return 1;
    } else {
        return 0;
    }
}

int buildTree(struct Tree T[]) {
    int N;
    scanf("%d\n", &N);
    int check[N];
    for (int i=0; i<N; ++i) {check[i] = 0;}
    char l, r;
    
    for (int i=0; i<N; ++i) {
        scanf("%c %c\n", &l, &r);
        T[i].data = i;
        if (l != '-') {
            T[i].left = l - '0';
            check[T[i].left] = 1;
        } else {T[i].left = -1;}
        if (r != '-') {
            T[i].right = r - '0';
            check[T[i].right] = 1;
        } else {T[i].right = -1;}
    }
    int root = -1;
    for (int i=0; i<N; ++i) {
            if (!check[i]) {
                root = i;
                break;
            }
    }
    return root;
}

void addQ(int pos_Tree, qptr Q) {
    struct Node* tNode = (struct Node*)malloc(sizeof(struct Node));
    tNode->treeIndex=pos_Tree;
    tNode->next = NULL;
    
    if (Q->head == NULL) {
        Q->head = tNode;
        Q->rear = tNode;
    } else {
        Q->rear->next =tNode;
        Q->rear = tNode;
    }
}

int delQ(qptr Q) {
    struct Node *headCell = Q->head;
    if (Q->head == Q->rear) {
        Q->head = Q->rear = NULL;
    } else {
        Q->head = Q->head->next;
    }
    int treeIndex = headCell->treeIndex;
    free(headCell);
    return treeIndex;
}

void print_leaf(int R) {
    qptr Q = (qptr)malloc(sizeof(struct QNode));
    Q->head = Q->rear = NULL;
    addQ(R, Q);
    int flag = 0;
    while (!isEmpty(Q)) {
        int pos_Tree = delQ(Q);
        if ((T[pos_Tree].left == -1) && 
            (T[pos_Tree].right == -1)) {
            if (!flag) {flag = 1;} else {printf(" ");}
            printf("%d", T[pos_Tree].data);
        }
        if (T[pos_Tree].left != -1) {
            addQ(T[pos_Tree].left, Q);
        }
        if (T[pos_Tree].right != -1) {
            addQ(T[pos_Tree].right, Q);
        }
    }
}

int main(int argc, char* argv[]) {
    int R = buildTree(T);
    print_leaf(R);
    return 0;
}